package com.jacken.mars.utils;

import java.util.concurrent.*;
import java.util.function.Supplier;

/**
 * @author wangqinag
 * @version 1.0
 * @date 2020/12/28 20:09
 */
public class DemonThread {
    public static void main(String[] args) {
       String content="[\"https://lk-img.oss-cn-hangzhou.aliyuncs.com/img/1600857165065.jpg?size=1080x1080\"]";
    }

    private static void test05() {
        System.out.println(Thread.currentThread().getName());
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            System.out.println("===task start===");
            try {

                System.out.println(Thread.currentThread().getName());

            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("===task finish===");
        });

        try {
            future.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void test04() {
        System.out.println(Thread.currentThread().getName());
        CompletableFuture.runAsync(() -> {
            System.out.println("===task start===");
            try {

                System.out.println(Thread.currentThread().getName());

            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("===task finish===");
        });


    }

    private static void test03() {
        System.out.println("main函数开始执行");
        ExecutorService executor = Executors.newFixedThreadPool(2);
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                System.out.println("===task start===");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("===task finish===");
                return 3;
            }
        }, executor);
        try {
            System.out.println("main函数执行结束:" + future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        executor.shutdown();
    }

    private static void test02() {
        System.out.println("main函数开始执行");

        ExecutorService executor = Executors.newFixedThreadPool(1);
        Future<Integer> future = executor.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("===task start===");
                Thread.sleep(5000);
                System.out.println("===task finish===");
                return 3;
            }
        });
        //这里需要返回值时会阻塞主线程，如果不需要返回值使用是OK的。倒也还能接收
        try {
            Integer result = future.get();
            System.out.println("main函数执行结束:" + result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        executor.shutdown();
    }

    private static void test01() {
        System.out.println("main函数开始执行");
        Thread thread = new Thread(() -> {
            System.out.println("===task start===");
            try {
                Thread.sleep(5000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("===task finish===");
        });


        thread.start();

        System.out.println("main函数执行结束");
    }
}
